// page/addtownid/addtownid.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tlist: [],
    townidlist: [],
    shenglist: [], //省列表
    shengid: '', //选择的省ID
    shengindex: '',
    shilist: [], //市列表
    shiid: '', //选择的市ID
    shiindex: '',
    xian: [], //县列表
  },
  // 点击省的表头
  tapsheng() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      shenglist: [],
      shengid: '', //选择的省ID
      shengindex: '',
      shilist: [],
      shiindex: '',
      shiid: '', //选择的市ID
      xian: [],
    })
    that.getsheng()
  },
  // 点击省份
  fixsheng(e) {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      shengid: e.currentTarget.dataset.shengid,
      shengindex: e.currentTarget.dataset.index
    })
    // that.getshi()
    that.tapshi()
  },
  // 点击市标头
  tapshi() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      shilist: [],
      shiid: '', //选择的市ID
      shiindex: '',
      xian: [],
    })
    that.getshi()
  },
  // 点击市
  fixshi(e) {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      shiid: e.currentTarget.dataset.shengid,
      shiindex: e.currentTarget.dataset.index
    })
    that.getxian()
    // that.tapshi()
  },
  // 点击城市名
  fixxian(e) {
    let that = this
    let xList = that.data.xian
    if (xList[e.currentTarget.dataset.index].actives) {
      that.dellist(xList[e.currentTarget.dataset.index])
    } else {
      that.addlist(xList[e.currentTarget.dataset.index])
    }
    xList[e.currentTarget.dataset.index].actives = !xList[e.currentTarget.dataset.index].actives
    that.setData({
      xian: xList
    })
  },
  cuohaodel(e) {
    let that = this
    let list = that.data.townidlist
    let xian = that.data.xian
    for (var index in xian) {
      if (xian[index].townId == list[e.currentTarget.dataset.index].townId) {
        xian[index].actives = false
      }
    }
    list.splice(e.currentTarget.dataset.index, 1)
    that.setData({
      townidlist: list,
      xian: xian
    })
    that.setList()
  },
  // 从列表删除
  dellist(obj) {
    let that = this
    let list = that.data.townidlist
    for (var index in list) {
      if (list[index].townId == obj.townId) {
        list.splice(index, 1)
      }
    }
    that.setData({
      townidlist: list
    })
    that.setList()
  },
  // 添加到列表
  addlist(obj) {
    let that = this
    let list = that.data.townidlist
    let addn = true
    for (var index in list) {
      if (list[index].townId == obj.townId) {
        addn = false
      }
    }
    if (addn) {
      list.push(obj)
    }
    console.log(list)
    that.setData({
      townidlist: list
    })
    that.setList()
  },
  setList() {
    let that = this
    let tlist = []
    let list = that.data.townidlist
    for (var index in list) {
      tlist.push(list[index].townId)
    }
    that.setData({
      tlist: tlist
    })
  },
  getsheng() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getProvinceInfo"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let shenglist = res.data.provinceList
          for (var index in res.data.provinceList) {
            res.data.provinceList[index].actives = false
          }
          that.setData({
            shenglist: shenglist
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }

      }
    });
  },
  getshi() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getCityInfo",
        provinceId: that.data.shengid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let cityList = res.data.cityList
          for (var index in res.data.cityList) {
            res.data.cityList[index].actives = false
          }
          that.setData({
            shilist: cityList
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  getxian() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getTownInfo",
        cityId: that.data.shiid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let townList = res.data.townList
          for (var index in townList) {
            townList[index].actives = false
            for (let j in that.data.tlist) {
              if (that.data.tlist[j] == townList[index].townId) {
                townList[index].actives = true
              }
            }
          }
          that.setData({
            xian: townList
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  queding() {
    let that = this
    let townList = that.data.townidlist
    console.log(townList)
    if (townList.length < 1) {
      wx.showToast({
        title: '至少选择一个销售区域',
        icon: 'none'
      })
      return
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    var prevPage = pages[pages.length - 2]; //获取上一个页面
    prevPage.setData({ //修改上一个页面的变量
      region: townList
    })
    //prevPage.loadData();
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let list = []
    let townlists = JSON.parse(options.townList)
    for (let i in townlists) {
      list.push(townlists[i].townId)
    }
    this.setData({
      townidlist: townlists,
      tlist: list
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.getsheng()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})